<template>
    <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <h2 class="text-center  margin-bottom">{{title}}</h2>
            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="收运时间">
                            <a-range-picker
                                    :show-time="{ format: 'HH:mm' }"
                                    :placeholder="['开始时间', '结束时间']"
                                    v-model="rangeTime"
                                    format="YYYY-MM-DD HH:mm"
                                    @change="handleDateChange"
                            />
                        </a-form-item>
                    </a-col>

                    <a-col :md="6" :sm="24" >
                        <a-form-item label="产生单位">
                            <select-customer-list ref="selectCustomerRef" @change="changeCustomer"></select-customer-list>
                        </a-form-item>
                    </a-col>

                    <a-col :md="6" :sm="24" v-has="'query:company:select:auth'">
                        <a-form-item label="">
                            <select-company-list :companysId="queryParam.companysId"  @change="changeCompany" ></select-company-list>
                        </a-form-item>
                    </a-col>

                    <a-col :md="4" :sm="24">
                        <a-form-item label="是否入库">
                            <a-select placeholder="入库状态" v-model="queryParam.isStorage">
                                <a-select-option value="">全部</a-select-option>
                                <a-select-option value="0">未入库</a-select-option>
                                <a-select-option value="1">已入库</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <a-col :md="8" :sm="24">
                        <a-form-item label="区域查询">
                            <select-area ref="selectAreaRef" @change="changeArea"></select-area>
                        </a-form-item>
                    </a-col>

<!--                    <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
<!--                        <span class="table-page-search-submitButtons">-->
<!--                              <a-button type="primary" @click="searchQuery" icon="search">-->
<!--                                  查询数据列表-->
<!--                              </a-button>-->
<!--                        </span>-->
<!--                    </a-col>-->

                </a-row>
            </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="table-operator" style="display: flex;justify-content: space-between;align-items: center;">
            <div>
                <a-button type="primary" @click="searchQuery" icon="search">查询数据列表</a-button>
                <a-button type="primary" @click="resetForm">重置查询</a-button>
            </div>
        </div>

        <!-- table区域-begin -->
        <div>
            <CheckboxWasteType :companysId="queryParam.companysId" @change="changeWasteType"></CheckboxWasteType>
        </div>

        <div>
            <a-table
                    ref="table"
                    size="middle"
                    bordered
                    rowKey="id"
                    :columns="columns"
                    :dataSource="dataSource"
                    :pagination="ipagination"
                    :loading="loading"
                    class="j-table-force-nowrap"
                    @change="handleTableChange"
            >
                <template slot="htmlSlot" slot-scope="text">
                    <div v-html="text"></div>
                </template>
                <template slot="link" slot-scope="text,record">
                    <a href="#" style="color: #3fe4f1;" @click="handleShowDetail(record)">详情</a>
                </template>
                <template slot="imgSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
                    <img v-else :src="text" height="25px" alt style="width: auto;height: 50px;"/>
                </template>
            </a-table>
        </div>

        <waste-trans-log-details ref="detailsRef" ></waste-trans-log-details>
    </a-card>
</template>

<script>
    /**
     * create by Jane
     * 二维码回溯
     */
    import '@/assets/less/TableExpand.less'
    import { mixinDevice } from '@/utils/mixin'
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import SelectArea from '@/components/Jane/SelectArea'
    import SelectCompanyList from '@/components/Jane/SelectCompanyList'
    import SelectCustomerList from '@/components/Jane/SelectCustomerList'
    import WasteTransLogDetails from './modules/WasteTransLogDetails'
    import CheckboxWasteType from '@/components/Jane/CheckboxWasteType'

    export default {
        mixins: [JeecgListMixin, mixinDevice],
        components: {
            WasteTransLogDetails,
            SelectArea,
            SelectCompanyList,
            SelectCustomerList,
            CheckboxWasteType,
        },
        data() {
            return {
                title : '废物收运记录',
                // 表头
                columns: [],
                url: {
                    list: '/hc/hcRecycleRecord/queryRecycleRecordPageList',
                },
                rangeTime : [],
                queryParam: {
                    creatTimeBegin: null,
                    creatTimeEnd: null,
                    customerName: '',
                    isTransfer: '',
                    isStorage: ''
                },
                dictOptions: {},
                wasteTypeList: [], // 废物类型列表 (原始数据)
                wasteId: []  // 选择的废物
            }
        },
        mounted() {
        },
        methods: {
            // 初始化数据
            initData: function() {
                for (let i in this.queryParam) {
                    this.queryParam[i] = ''
                }
                // 获取到用户登陆的信息
                let userInfo = this.$store.getters.userInfo;
                if(userInfo.companysId){
                    this.queryParam.companysId = userInfo.companysId;
                }
                // 如果用户存在区县编码、处置单位Id编号，则赋值
                if (userInfo.cityCode) {
                    this.queryParam.cityCode = userInfo.cityCode
                }
                if (userInfo.areaCode) {
                    this.queryParam.areaCode = userInfo.areaCode
                }
                let ref = this.$refs.selectAreaRef
                if (ref && ref.clearSelectArea != undefined) {
                    ref.clearSelectArea()
                }
                let refCus = this.$refs.selectCustomerRef;
                if(refCus && refCus.clearSelectCustomer != undefined){
                    refCus.clearSelectCustomer();
                }

                // 时间初始化
                let date = new Date()
                let time1 = new Date(date.setHours(0, 0, 0, 0)).Format('yyyy-MM-dd hh:mm')
                let time2 = new Date(date.setHours(0, 0, 0, 0) + (1000 * 60 * 60 * 24 - 1)).Format('yyyy-MM-dd hh:mm')
                this.queryParam.creatTimeBegin = time1;
                this.queryParam.creatTimeEnd = time2;
                this.rangeTime = [time1, time2];
            },

            async initDictConfig() {
                this.initData()
                this.setColumns()
            },


            handleDateChange(value, dateString) {
                this.rangeTime = dateString
                if (dateString[0]) {
                    this.queryParam.creatTimeBegin = dateString[0]
                }
                if (dateString[1]) {
                    this.queryParam.creatTimeEnd = dateString[1]
                }
            },
            async changeArea({ area, city }) {
                if (city) {
                    this.queryParam.cityCode = city
                }
                if (area) {
                    this.queryParam.areaCode = area
                }
            },
            async changeCustomer(value,option) {
                this.queryParam.customerNumber = value;
            },
            changeCompany(value,option) {
                this.queryParam.companysId = value;
                this.searchQuery()
            },

            // 废物类型选择改变
            changeWasteType(info,wasteList) {
                this.wasteId = info.ids;
                this.queryParam.wasteId = info.ids.join(',');
                this.wasteTypeList = wasteList;
                this.setColumns();
                // this.loadData()
                this.searchQuery()
            },
            // 查询数据列表
            searchQuery: function() {
                this.queryParam.wasteId = this.wasteId.join(',')
                this.loadData(1)
            },
            // 重置筛选表单
            resetForm: function() {
                this.initData()
                this.searchQuery();
            },

            // 根据机构编号查询机构超时提交信息详情
            handleShowDetail : function(row) {
                if(row){
                    let ref = this.$refs.detailsRef;
                    if(ref){
                        ref.show(row);
                    }
                }
            },
            // 设置列
            setColumns() {
                let cols = []
                let data = this.wasteTypeList;
                data.forEach((item, index) => {
                    if (item.wasteTypeName) {
                        let child = [];
                        (item['children'] || []).forEach((val, i) => {
                            let info = this.wasteId.find(v => v === val.id)
                            if (info) {

                                child.push({
                                    title: `${val.wasteTypeName}${val.priceUnit ? `(${val.priceUnit})` : ''}`,
                                    dataIndex: 'id',
                                    align: 'center',
                                    key: `${index}-${i}`,
                                    customRender: function(vv, waste, index) {
                                        let info = (waste['list']).find((v) => v.wasteId === val.id)
                                        if (info) {
                                            const obj = {
                                                children: <div style="font-weight:600;">{info.wastePresentValue || 0} {val.priceUnit || ''}</div>,
                                                attrs: {}
                                            }
                                            return obj
                                        }
                                        return ' '
                                    }
                                })
                            }
                        })
                        if(child.length){
                            let obj = {
                                title: item.wasteTypeName,
                                dataIndex: 'id',
                                key: `${index}`,
                                children: child
                            }
                            cols.push(obj)
                        }
                    }
                })

                this.columns = [
                    {
                        title: '序号',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 60,
                        align: 'center',
                        customRender: function(t, r, index) {
                            return parseInt(index) + 1
                        }
                    },
                    {
                        title: '收运编号',
                        align: 'center',
                        dataIndex: 'recycleNumber',
                    },
                    {
                        title: '产生单位',
                        align: 'center',
                        dataIndex: 'customerName'
                    },
                    {
                        title: '收运人',
                        align: 'center',
                        dataIndex: 'recycleOriginalPersonName',
                        customRender: function(value, row, index) {
                            return value || row['recyclePersonName'] || '--'
                        }
                    },
                    {
                        title: '收运时间',
                        align: 'center',
                        dataIndex: 'createTime',
                        customRender: function(value, row, index) {
                            return value || ' '
                        }
                    },
                    ...cols,
                    {
                        title: '运输车辆',
                        align: 'center',
                        dataIndex: 'carNumber',
                        customRender: function(value, row, index) {
                            return value || ' '
                        }
                    },
                    {
                        title: '是否入库',
                        align: 'center',
                        dataIndex: 'isStorage',
                        scopedSlots: { customRender: 'htmlSlot' },
                        customRender: function(value, row, index) {
                            const obj = {
                                children: ' ',
                                attrs: {}
                            }
                            if (value == 0) {
                                obj['children'] =
                                    <
                                        span
                                        style = 'color:#05f305;' >未入库< /span>
                            }
                            else if (value == 1) {
                                        obj['children'] =
                                            <
                                                span
                                                style = 'color:orange;' >已入库< /span>
                            }
                            return obj
                        }
                    },
                                                {
                                                    title: '备注',
                                                    align: 'center',
                                                    dataIndex: 'remarks',
                                                    customRender: function(value, row, index) {
                                                    return value || ' '
                                                }
                                                },
                                                {
                                                    title: '转运详情',
                                                    align: 'center',
                                                    dataIndex: 'id',
                                                    scopedSlots: { customRender: 'link' },
                                                },
                ]
            },
        }
    }


</script>
<style scoped>
    @import '~@assets/less/common.less';
</style>
